//
//  GMPlayerViewController.m
//  StockSystem
//
//  Created by 小熊 on 2018/6/13.
//  Copyright © 2018年 小熊. All rights reserved.
//

#import "GMPlayerViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import "ZFPlayer.h"
#import "UINavigationController+ZFFullscreenPopGesture.h"
#import "GMKnowlegeCell.h"
#import "JYJBackViewCell.h"
#import "GMQuestionViewController.h"
@interface GMPlayerViewController ()<ZFPlayerDelegate>
@property (weak, nonatomic) IBOutlet UIView *fatherView;

@property (nonatomic ,strong) ZFPlayerView  *playerView;
/** 离开页面时候是否在播放 */
@property (nonatomic, assign) BOOL isPlaying;
@property (nonatomic, strong) ZFPlayerModel *playerModel;
@property (nonatomic, strong) NSArray *titArr;
@property (weak, nonatomic) IBOutlet UIView *titView;
@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;
/* 上一次选择的按钮 */
@property (weak , nonatomic)UIButton *lastSelectButton;


@end
/** 重用cell ID */
static NSString *const CollectionCell = @"CollectionCell";
/** 按钮tag附加值 */
#define BTNTAG 1000;
@implementation GMPlayerViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // pop回来时候是否自动播放
    if (self.navigationController.viewControllers.count == 2 && self.playerView && self.isPlaying) {
        self.isPlaying = NO;
        self.playerView.playerPushedOrPresented = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // push出下一级页面时候暂停
    if (self.navigationController.viewControllers.count == 3 && self.playerView && !self.playerView.isPauseByUser)
    {
        self.isPlaying = YES;
        //        [self.playerView pause];
        self.playerView.playerPushedOrPresented = YES;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zf_prefersNavigationBarHidden = YES;
    // 自动播放，默认不自动播放
    [self.playerView autoPlayTheVideo];
    [self setUpAllChildViewController];
}
#pragma mark - 添加所有子控制器
- (void)setUpAllChildViewController
{
    NSArray *titles = @[@"测试01",@"测试02",@"测试03",@"测试04"];
    for (NSInteger i = 0; i < titles.count; i++) {
        UIViewController *vc = [UIViewController new];
        vc.title = titles[i];
        vc.view.backgroundColor = [UIColor zj_randomColor]; //随机色
        [self addChildViewController:vc];
    }
}

// 返回值要必须为NO
- (BOOL)shouldAutorotate {
    return NO;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    // 这里设置横竖屏不同颜色的statusbar
    // if (ZFPlayerShared.isLandscape) {
    //    return UIStatusBarStyleDefault;
    // }
    return UIStatusBarStyleLightContent;
}

- (BOOL)prefersStatusBarHidden {
    return ZFPlayerShared.isStatusBarHidden;
}

#pragma mark - ZFPlayerDelegate

- (void)zf_playerBackAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)zf_playerControlViewWillShow:(UIView *)controlView isFullscreen:(BOOL)fullscreen {
    //    self.backBtn.hidden = YES;
    [UIView animateWithDuration:0.25 animations:^{
//        self.backBtn.alpha = 0;
    }];
}

- (void)zf_playerControlViewWillHidden:(UIView *)controlView isFullscreen:(BOOL)fullscreen {
    //    self.backBtn.hidden = fullscreen;
    [UIView animateWithDuration:0.25 animations:^{
//        self.backBtn.alpha = !fullscreen;
    }];
}

#pragma mark - Getter

- (ZFPlayerModel *)playerModel {
    if (!_playerModel) {
        _playerModel                  = [[ZFPlayerModel alloc] init];
        _playerModel.title            = @"这里设置视频标题";
        _playerModel.videoURL         = [NSURL URLWithString:@"http://static.smartisanos.cn/common/video/proud-farmer.mp4"];
        _playerModel.placeholderImage = [UIImage imageNamed:@"loading_bgView1"];
        _playerModel.fatherView       = self.fatherView;
        //        _playerModel.resolutionDic = @{@"高清" : self.videoURL.absoluteString,
        //                                       @"标清" : self.videoURL.absoluteString};
    }
    return _playerModel;
}

- (ZFPlayerView *)playerView {
    if (!_playerView) {
        _playerView = [[ZFPlayerView alloc] init];
        
        /*****************************************************************************************
         *   // 指定控制层(可自定义)
         *   // ZFPlayerControlView *controlView = [[ZFPlayerControlView alloc] init];
         *   // 设置控制层和播放模型
         *   // 控制层传nil，默认使用ZFPlayerControlView(如自定义可传自定义的控制层)
         *   // 等效于 [_playerView playerModel:self.playerModel];
         ******************************************************************************************/
        [_playerView playerControlView:nil playerModel:self.playerModel];
        
        // 设置代理
        _playerView.delegate = self;
        
        //（可选设置）可以设置视频的填充模式，内部设置默认（ZFPlayerLayerGravityResizeAspect：等比例填充，直到一个维度到达区域边界）
        // _playerView.playerLayerGravity = ZFPlayerLayerGravityResize;
        
        // 打开下载功能（默认没有这个功能）
//        _playerView.hasDownload    = YES;
        
        // 打开预览图
        _playerView.hasPreviewView = YES;
        
        //        _playerView.forcePortrait = YES;
        /// 默认全屏播放
        //        _playerView.fullScreenPlay = YES;
        
    }
    return _playerView;
}



- (void)dealloc {
    NSLog(@"%@释放了",self.class);
}
- (IBAction)selectBtn:(UIButton *)sender {
     NSInteger buttonTag = sender.tag - BTNTAG;
    [self selectButton:sender];
    //添加控制器View
    [self AddOneVcWithButtton:buttonTag];
    //滚动到相应的位置
    _contentScrollView.contentOffset = CGPointMake(buttonTag * DEVICE_WIDTH, 0);
    
}
#pragma mark - 选中标题
- (void)selectButton:(UIButton *)button
{
    _lastSelectButton.transform = CGAffineTransformIdentity; //还原
    _lastSelectButton.selected = NO;
    button.selected =YES;
    _lastSelectButton = button;
    
    
}
//添加控制器View
- (void)AddOneVcWithButtton:(NSInteger)i
{
    UIViewController *vc = self.childViewControllers[i];
    if (vc.view.superview)return;
    vc.view.frame = CGRectMake(i * DEVICE_WIDTH, 0,DEVICE_WIDTH , _contentScrollView.zj_height);
    [_contentScrollView addSubview:vc.view];
}


@end
